﻿using System;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.IO;
using System.Collections.Generic;

/// <summary>
/// Summary description for EmailSender
/// </summary>
namespace SaleOffVn.Web.Career
{
    public class EmailSender
    {
        public EmailSender()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static void SendMail(string sMailServer, string sMailFrom, List<string> MailTo, string sSenderName, string sSenderEmail, string sSenderAddress, string sSubject, string sBody)
        {
            // Create Msg
            MailMessage ContactMail = new System.Net.Mail.MailMessage();
            ContactMail.From = new MailAddress(sMailFrom);
            foreach (string sEmailAddress in MailTo)
            {
                ContactMail.To.Add(new MailAddress(sEmailAddress));
            }
            ContactMail.Subject = sSubject;
            ContactMail.BodyEncoding = System.Text.Encoding.UTF8;
            ContactMail.Body = sBody;

            //Send Msg
            SmtpClient smtp = new SmtpClient();
            smtp.Host = sMailServer;
            try
            {
                smtp.Send(ContactMail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //public static void SendMail(string sMailServer, string sAccName, string sAccPass, List<string> MailTo, string sSenderName, string sSenderEmail, string sSenderAddress, string sSubject, string sBody)
        //{
        //    // Create Msg
        //    MailMessage ContactMail = new System.Net.Mail.MailMessage();
        //    ContactMail.From = new MailAddress("info@psdus.com");
        //    //ContactMail.From = new MailAddress(sSenderEmail, sSenderName);
        //    foreach (string sEmailAddress in MailTo)
        //    {
        //        ContactMail.To.Add(new MailAddress(sEmailAddress));
        //    }
        //    ContactMail.Subject = sSubject;
        //    ContactMail.BodyEncoding = System.Text.Encoding.UTF8;
        //    ContactMail.Body = sBody;

        //    //Send Msg
        //    SmtpClient smtp = new SmtpClient();
        //    smtp.Host = sMailServer;
        //    //smtp.Credentials = new NetworkCredential(sAccName, sAccPass);
        //    try
        //    {
        //        smtp.Send(ContactMail);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        public static void SendMail(string subject, string mailFrom, List<string> MailTo, string templateFile, string mailServer)
        {
            // Create Msg
            MailMessage ContactMail = new System.Net.Mail.MailMessage();
            ContactMail.From = new MailAddress(mailFrom);
            foreach (string sEmailAddress in MailTo)
            {
                ContactMail.To.Add(new MailAddress(sEmailAddress));
            }
            ContactMail.Subject = subject;

            // build the message body
            StringBuilder buf = null;

            using (StreamReader sr = new StreamReader(templateFile))
            {
                buf = new StringBuilder(sr.ReadToEnd());
            }
            ContactMail.Body = buf.ToString();
            ContactMail.IsBodyHtml = true;
            ContactMail.BodyEncoding = System.Text.Encoding.UTF8;

            //Send Msg
            SmtpClient smtp = new SmtpClient();
            smtp.Host = mailServer;
            try
            {
                smtp.Send(ContactMail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //public static void SendMail(string subject, string mailFrom, List<string> MailTo, string templateFile, string mailServer, string sAccName, string sAccPass)
        //{
        //    // Create Msg
        //    MailMessage ContactMail = new System.Net.Mail.MailMessage();
        //    ContactMail.From = new MailAddress("info@psdus.com");
        //    //ContactMail.From = new MailAddress(mailFrom);
        //    foreach (string sEmailAddress in MailTo)
        //    {
        //        ContactMail.To.Add(new MailAddress(sEmailAddress));
        //    }
        //    ContactMail.Subject = subject;

        //    // build the message body
        //    StringBuilder buf = null;

        //    using (StreamReader sr = new StreamReader(templateFile))
        //    {
        //        buf = new StringBuilder(sr.ReadToEnd());
        //    }
        //    ContactMail.Body = buf.ToString();
        //    ContactMail.IsBodyHtml = true;
        //    ContactMail.BodyEncoding = System.Text.Encoding.UTF8;

        //    //Send Msg
        //    SmtpClient smtp = new SmtpClient();
        //    smtp.Host = mailServer;
        //    //smtp.Credentials = new NetworkCredential(sAccName, sAccPass);
        //    try
        //    {
        //        smtp.Send(ContactMail);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}


        public static void SendMail(string mailServer, string subject, string mailFrom, List<string> MailTo, string fileName, string templateFile)
        {
            // Create Msg
            MailMessage ContactMail = new System.Net.Mail.MailMessage();
            ContactMail.From = new MailAddress(mailFrom);
            foreach (string sEmailAddress in MailTo)
            {
                ContactMail.To.Add(new MailAddress(sEmailAddress));
            }
            ContactMail.Subject = subject;
            ContactMail.Attachments.Add(new Attachment(fileName));

            // build the message body
            StringBuilder buf = null;
            using (StreamReader sr = new StreamReader(templateFile))
            {
                buf = new StringBuilder(sr.ReadToEnd());
            }
            ContactMail.Body = buf.ToString();
            ContactMail.IsBodyHtml = true;

            //Send Msg
            SmtpClient smtp = new SmtpClient();
            smtp.Host = mailServer;
            try
            {
                smtp.Send(ContactMail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //public static void SendMail(string mailServer, string sAccName, string sAccPass, string subject, string mailFrom, List<string> MailTo, string fileName, string templateFile)
        //{
        //    // Create Msg
        //    MailMessage ContactMail = new System.Net.Mail.MailMessage();
        //    ContactMail.From = new MailAddress("info@psdus.com");
        //    //ContactMail.From = new MailAddress(mailFrom);
        //    foreach (string sEmailAddress in MailTo)
        //    {
        //        ContactMail.To.Add(new MailAddress(sEmailAddress));
        //    }
        //    ContactMail.Subject = subject;
        //    ContactMail.Attachments.Add(new Attachment(fileName));

        //    // build the message body
        //    StringBuilder buf = null;
        //    using (StreamReader sr = new StreamReader(templateFile))
        //    {
        //        buf = new StringBuilder(sr.ReadToEnd());
        //    }
        //    ContactMail.Body = buf.ToString();
        //    ContactMail.IsBodyHtml = true;

        //    //Send Msg
        //    SmtpClient smtp = new SmtpClient();
        //    smtp.Host = mailServer;
        //    //smtp.Credentials = new NetworkCredential(sAccName, sAccPass);
        //    try
        //    {
        //        smtp.Send(ContactMail);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}
    }
}
